﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CreatePGSQLDataScript
{
    public partial class ScriptDebugger : Form
    {
        public ScriptDebugger()
        {
            InitializeComponent();
        }


        DataAccess.PgSqlProvider db1;
        private void ScriptDebugger_Load(object sender, EventArgs e)
        {
            //cmbConn1.Items.Add("server=192.168.1.58;database=justgogopg11;uid=postgres;pwd=`12345;MaxPoolSize=500;");
            historyList.ReadList(cmbConn1);
        }

        private void btnConn_Click(object sender, EventArgs e)
        {
            db1 = new DataAccess.PgSqlProvider(cmbConn1.Text);
            db1.Open();
            historyList.SaveValue(cmbConn1);
            MessageBox.Show("连接成功！");
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            try
            {
                string currScript = rtxtScript.Text;

                string script = string.Format(@"
CREATE OR REPLACE FUNCTION tempforkevindebugger()
  RETURNS varchar(1000)  AS
$BODY$
{0}
$BODY$
  LANGUAGE 'plpgsql' VOLATILE", currScript);

                db1.ExecuteSQL(script);

                string result = db1.ExecuteScalar("select tempforkevindebugger()").ToString();
                rtxtResult.Text = result;

            }
            catch (Exception ex)
            {
                rtxtResult.Text = ex.Message;
            }

        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            string sqlText = "DROP FUNCTION tempforkevindebugger();";
            db1.ExecuteSQL(sqlText);
            rtxtResult.Text = "FUNCTION tempforkevindebugger 清除成功!";
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }
    }
}
